package com.itheima.service.impl;

import com.itheima.entity.OperateLog;
import com.itheima.mapper.OperateLogMapper;
import com.itheima.service.OperateLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OperateLogServiceImpl implements OperateLogService {

    @Autowired
    private OperateLogMapper operateLogMapper;

    @Override
    public List<OperateLog> getLogPage(int page, int pageSize) {
        int offset = (page - 1) * pageSize;
        List<OperateLog> logs = operateLogMapper.getLogPage(offset, pageSize);

        for (OperateLog log : logs) {
            if (log.getOperateEmpId() != null) {
                String empName = operateLogMapper.getEmpNameById(log.getOperateEmpId());
                log.setOperateEmpName(empName != null ? empName : "未知");
            } else {
                log.setOperateEmpName("未知");
            }
        }

        return logs;
    }

    @Override
    public long getTotalLogs() {
        return operateLogMapper.getTotalLogs();
    }

    @Override
    public void insert(OperateLog log) {
        operateLogMapper.insert(log);
    }
}